﻿using DongFangGuoKai.WCSPlatform.WebApi.Utils.UtilModel;
using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using DongFangGuoKai.WMSPlatform.Commn.HttpModule;
using S7.Net;

namespace DongFangGuoKai.WCSPlatform.WebApi.ExternalSystem.AGVCommon
{
    /// <summary>
    /// AGV
    /// </summary>
    public class AGVHttpClient
    {
        private readonly string agvBaseUrl;
        private readonly Plc plc;
        private readonly PlcDBAddress plcDBAddress;
        private readonly ILogger<AGVHttpClient> logger;
        private readonly ICtmHttpClient httpClient;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="agvBaseUrl"></param>
        /// <param name="plc"></param>
        /// <param name="plcDBAddress"></param>
        /// <param name="logger"></param>
        /// <param name="httpClient"></param>
        public AGVHttpClient(string agvBaseUrl, Plc plc,PlcDBAddress plcDBAddress, ILogger<AGVHttpClient> logger,ICtmHttpClient httpClient)
        {
            this.agvBaseUrl = agvBaseUrl;
            this.plc = plc;
            this.plcDBAddress = plcDBAddress;
            this.logger = logger;
            this.httpClient = httpClient;
        }
        /// <summary>
        /// 料箱取放回调
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public AGVBaseResModel BoxApplyPass(BoxApplyPassReq model,out string httpMsg)
        {
            try
            {
                var requestUrl = agvBaseUrl + "/rcms/services/rest/hikRpcService/boxApplyPass";
                var res = httpClient.Post<AGVBaseResModel>(requestUrl, model);
                httpMsg = $"CTU料箱取放回调:[url:{requestUrl}],[入参:{model.ToJson()}],[出参:{res.ToJson()}]";
                return res;
            }
            catch (Exception)
            {
                throw;
            }

            if (string.IsNullOrEmpty(model.TaskCode))
                return new AGVBaseResModel("1", "参数taskCode为空", model.ReqCode);

           
            //return httpClient.Post<AGVBaseResModel>(requestUrl, model); //测试使用
            try
            {
                Task<bool> task; // 声明一个用来存储异步任务的变量
                if (model.Type == "1")
                {
                    task = Task.Run(() =>
                    {
                        bool isPick = false;
                        do
                        {
                            isPick = (bool)plc.Read(plcDBAddress.AgvPick);
                        } while (!isPick);
                        return isPick; // 返回结果
                    });
                }
                else if (model.Type == "2")
                {
                    task = Task.Run(() =>
                    {
                        bool isDrop = false;
                        do
                        {
                            isDrop = (bool)plc.Read(plcDBAddress.AgvPlace);
                        } while (!isDrop);
                        return isDrop; // 返回结果
                    });
                }
                else
                {
                    return new AGVBaseResModel("1", "type参数值错误：" + model.Type, model.ReqCode);
                }

                var result = task.GetAwaiter().GetResult(); // 等待异步任务完成并获取结果

                logger.LogInformation("回调AGV入参" + model.ToJson());
                return new AGVBaseResModel("0", "成功", model.ReqCode);
                //return httpClient.Post<AGVBaseResModel>(requestUrl, model);
            }
            catch (Exception)
            {
                throw;
            }

        }

    }
}
